<?php
/**
 * Created by PhpStorm.
 * User: 72925
 * Date: 2018/11/23
 * Time: 9:26
 * order: 验证码操作
 */

namespace app\index\controller;
use think\Controller;
use app\index\model\User;
use think\Validate;
use lib\Phone;
use think\Cache;
class Code extends Controller
{

	/*接收手机号码并检验手机号码是否正确*/
	public function recepte()
	{	
		/*接收手机号码*/
			$mobile = trim(input('mobile'));
			$users = new User;
	        $res = $users->where(['mobile'=>$mobile])->find();
	    	if($res['mobile'] == NULL)
	        {	
	        		$phone = New Phone();
	        		$status = $phone ->phone($mobile);
	        		$sta = json_decode($status,true);
	       			$code = $sta["resp"]["respCode"];
	       			$code = "000000";
	        		if($code == "000000"){
	        			$param = Cache::get('param');
		        		$dat = array('code'=>200,'msg'=>'验证码已发送');
	                    return json_encode($dat,JSON_FORCE_OBJECT);
	        		}else{
	        			$dat = array('code'=>400,'msg'=>'验证码发送失败');
	                    return json_encode($dat,JSON_FORCE_OBJECT);
	        		}
	        		
	        		
	        		
                	
		    }   
	        elseif($res['mobile'])
	        {
		        	/*已经注册或者存在返回错误信息*/
		        	$dat = array('code'=>401,'msg'=>'手机号码已经注册');
	             	return json_encode($dat,JSON_FORCE_OBJECT);
	        }
	       
	}


	public function send()
	{	
		/*接收手机号码*/
			$mobile = trim(input('mobile'));
			$users = new User;
	        $res = $users->where(['mobile'=>$mobile])->find();
	    	if($res['mobile'] !== NULL)
	        {		
	        		$phone = New Phone();
	        		$status = $phone ->phone($mobile);
	        		$sta = json_decode($status,true);
	       			$code = $sta["resp"]["respCode"];
	       			$code = "000000";
	        		if($code == "000000"){
	        			$param = Cache::get('param');
		        		$dat = array('code'=>200,'msg'=>'验证码已发送');
	                    return json_encode($dat,JSON_FORCE_OBJECT);
	        		}else{
	        			$dat = array('code'=>400,'msg'=>'验证码发送失败');
	                    return json_encode($dat,JSON_FORCE_OBJECT);
	        		}
	        		
	        		                	
		    }   
	        elseif($res == NULL)
	        {
		        	/*已经注册或者存在返回错误信息*/
		        	$dat = array('code'=>401,'msg'=>'此手机号码未注册');
	             	return json_encode($dat,JSON_FORCE_OBJECT);

	        }
	       
	}
	
}
